class Solution:
    def calculate(self, s: str) -> int:
        tokens = [[]]
        for c in s:
            if c in '+-*/':
                tokens.append([c])
                tokens.append([])
            elif str.isdigit(c):
                tokens[-1].append(c)
        tokens = [''.join(token) for token in tokens]
        tokens = [t if t in '+-*/' else int(t) for t in tokens]
        stack = []
        for token in tokens:
            if len(stack) == 0:
                stack.append(token)
                continue
            if isinstance(token, int) and stack[-1] in '*/':
                op = stack.pop()
                a = stack.pop()
                b = token
                if op == '*':
                    c = a * b
                else:
                    isneg = (a < 0 < b or b < 0 < a)
                    c = abs(a) // abs(b)
                    if isneg:
                        c = -c
                stack.append(c)
            else:
                stack.append(token)

        result = None
        for i, token in enumerate(stack):
            if i == 0:
                result = token
                continue
            if isinstance(token, int):
                op = stack[i - 1]
                if op == '+':
                    result = result + token
                else:
                    result = result - token
        return result

solution = Solution()
cases = [
    '1+2+3',
    '1-2+3',
    '1/2+3',
    '1+2-3/4',
    '1+2-3*4/5',
    ' 1 + 2- 3*4/5',
    '1*2 + 3*4/5 + 10/2*3 -1',
    '1-3/2',
    "3+2*2",
    " 3/2 ",
    " 3+5 / 2 "
]
for case in cases:
    res1 = solution.calculate(case)
    res2 = eval(case.replace('/', '//'))
    if res1 != res2:
        print(case, res1, res2)